package cn.tedu.zx.album.adapter.controller;


import cn.tedu.zx.album.service.AlbumService;
import cn.tedu.zx.common.restful.JsonPage;
import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.common.restful.ResponseCode;
import cn.tedu.zx.pojo.court.dto.AlbumAddNewDto;
import cn.tedu.zx.pojo.court.dto.AlbumUpdateDto;
import cn.tedu.zx.pojo.court.vo.AlbumDetailsVo;
import cn.tedu.zx.pojo.court.vo.AlbumListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/album")
//通过knife4j配置IPI文档
@Api(tags = "02.相册模块")
@Validated/*未封装参数检查注解*/
public class AlbumController {
    @Autowired
    private AlbumService service;

    @PostMapping("/addNew")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/album/insert')")
    public JsonResult<Void> addNew(@Valid AlbumAddNewDto albumAddNewDto) {
        log.debug("开始执行{}", albumAddNewDto);
        service.addNew(albumAddNewDto);
        log.debug("添加完成");
        return JsonResult.ok();//静态方法
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/album/delete')")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "albumId", value = "相册id", required = true, dataType = "Long")}
    )
    public JsonResult<Void> delete(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long albumId) {
        service.deleteById(albumId);
        return JsonResult.failed(ResponseCode.OK, "成功删除");
    }

    @GetMapping("/selectDetails")
    @ApiOperation("根据id查询相册详情")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "albumId", value = "相册id", required = true, dataType = "Long")}
    )
    public JsonResult<AlbumDetailsVo> selectByI(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long albumId) {
        AlbumDetailsVo albumDetailsVO = service.selectDetailsById(albumId);
        return JsonResult.ok(albumDetailsVO);
    }

    @PostMapping("/update")
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/album/update')")
    public JsonResult<Void> update(@Valid AlbumUpdateDto updateDto) {
        service.updateById(updateDto);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("相册列表")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "场地页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, dataType = "Integer")
    })
    public JsonResult<JsonPage<AlbumListVo>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        log.debug("开始处理请求{}", pageNum);
        JsonPage<AlbumListVo> jsonPage = service.list(pageNum, pageSize);
        log.debug("查询完成{}", jsonPage);
        return JsonResult.ok(jsonPage);
    }

}
